using System;
using System.Collections;
using System.Data.Linq.Mapping;
using System.Runtime.Serialization;
using Portal.Data.Helper;
using Portal.Core.Binding.FormBuilder;
using Portal.Core.Helper;
using System.Linq;
using Portal.Core.Utils;

namespace Portal.Data.Entity
{
	[Table(Name = "dbo.SimpleWebpart"),Serializable, DataContractAttribute]
    public class SimpleWebpart : BaseObject
	{
	    public SimpleWebpart()
	    {
	        ModifiedDate = DateTime.Now;
	    }

	    #region Properties
        [SkipBinding]
		[ColumnAttribute(Name = "ID", IsPrimaryKey = true , IsDbGenerated = true)]
		public int ID {get;set;}

		[ColumnAttribute(Name = "Title")]
		public string Title {get;set;}

		[ColumnAttribute(Name = "Body")
        , Validator(Required = true), TextBoxMode(Mode = TextMode.RichTextBox), RadEditor(ImagesPath = "Images/SimpleWebpart/", ToolFile = RadEditorAttribute.ToolFileEnum.Fullset), Wide]
		public string Body {get;set;}

		[ColumnAttribute(Name = "ModifiedDate"),SkipBinding]
		public DateTime ModifiedDate {get;set;}


		#endregion

		#region Methods
		public static SimpleWebpart FindByPrimaryKey(object objectid)
		{
		    return FindByPrimaryKey<SimpleWebpart>(objectid);
		}
        public static IList Paging(int pagesize, int pageindex, out int count)
        {
            var parameter = new object[] { typeof(SimpleWebpart).Name, "Paging", pagesize, pageindex };
            var obj = CacheHelper.Instance.Get(parameter);
            if (obj != null)
            {
                var values = (object[])obj;
                count = (int)values[1];
                return (IList)values[0];
            }

            //end get cache -------------------
            const string hql = "select * from SimpleWebpart";
            const string order = "order by ID desc";
            var list = LINQHelper.SlicedFindAll<SimpleWebpart>(hql, order, pageindex * pagesize, pagesize, out count);
            var newvalues = new object[] { list, count };
            CacheHelper.Instance.Set(parameter, newvalues);

            return list;
        }
        public static IList ListAll()
        {
            //getcache  -----------------------
            var parameter = new object[] { typeof(SimpleWebpart).Name, "ListAll" };
            var obj = CacheHelper.Instance.Get(parameter);
            if (obj == null)
            {
                var result = LINQHelper.GetList<SimpleWebpart>("select * from SimpleWebpart order by Title");
                CacheHelper.Instance.Set(parameter, result);
                return result;
            }
            return (IList)obj;
        }
		#endregion Methods
	}
}
